home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zcie.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  21.1 KB  |  692 lines

  1. /* Copyright (C) 1992, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zcie.c,v 1.4 2000/09/19 19:00:52 lpd Exp $ */
  20. /* CIE color operators */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "oper.h"
  25. #include "gsstruct.h"
  26. #include "gxcspace.h"        /* gscolor2.h requires gscspace.h */
  27. #include "gscolor2.h"
  28. #include "gscie.h"
  29. #include "estack.h"
  30. #include "ialloc.h"
  31. #include "idict.h"
  32. #include "idparam.h"
  33. #include "igstate.h"
  34. #include "icie.h"
  35. #include "isave.h"
  36. #include "ivmspace.h"
  37. #include "store.h"        /* for make_null */
  38.  
  39. /* Empty procedures */
  40. static const ref empty_procs[4] =
  41. {
  42.     empty_ref_data(t_array, a_readonly | a_executable),
  43.     empty_ref_data(t_array, a_readonly | a_executable),
  44.     empty_ref_data(t_array, a_readonly | a_executable),
  45.     empty_ref_data(t_array, a_readonly | a_executable)
  46. };
  47.  
  48. /* ------ Parameter extraction utilities ------ */
  49.  
  50. /* Get a range array parameter from a dictionary. */
  51. /* We know that count <= 4. */
  52. int
  53. dict_ranges_param(const ref * pdref, const char *kstr, int count,
  54.           gs_range * prange)
  55. {
  56.     int code = dict_floats_param(pdref, kstr, count * 2,
  57.                  (float *)prange, NULL);
  58.  
  59.     if (code < 0)
  60.     return code;
  61.     else if (code == 0)
  62.     memcpy(prange, Range4_default.ranges, count * sizeof(gs_range));
  63.     return 0;
  64. }
  65.  
  66. /* Get an array of procedures from a dictionary. */
  67. /* We know count <= countof(empty_procs). */
  68. int
  69. dict_proc_array_param(const ref * pdict, const char *kstr,
  70.               uint count, ref * pparray)
  71. {
  72.     ref *pvalue;
  73.  
  74.     if (dict_find_string(pdict, kstr, &pvalue) > 0) {
  75.     uint i;
  76.  
  77.     check_array_only(*pvalue);
  78.     if (r_size(pvalue) != count)
  79.         return_error(e_rangecheck);
  80.     for (i = 0; i < count; i++) {
  81.         ref proc;
  82.  
  83.         array_get(pvalue, (long)i, &proc);
  84.         check_proc_only(proc);
  85.     }
  86.     *pparray = *pvalue;
  87.     } else
  88.     make_const_array(pparray, a_readonly | avm_foreign,
  89.              count, &empty_procs[0]);
  90.     return 0;
  91. }
  92.  
  93. /* Get 3 ranges from a dictionary. */
  94. int
  95. dict_range3_param(const ref *pdref, const char *kstr, gs_range3 *prange3)
  96. {
  97.     return dict_ranges_param(pdref, kstr, 3, prange3->ranges);
  98. }
  99.  
  100. /* Get a 3x3 matrix from a dictionary. */
  101. int
  102. dict_matrix3_param(const ref *pdref, const char *kstr, gs_matrix3 *pmat3)
  103. {
  104.     /*
  105.      * We can't simply call dict_float_array_param with the matrix
  106.      * cast to a 9-element float array, because compilers may insert
  107.      * padding elements after each of the vectors.  However, we can be
  108.      * confident that there is no padding within a single vector.
  109.      */
  110.     float values[9];
  111.     int code;
  112.  
  113.     memcpy(&values[0], &Matrix3_default.cu, 3 * sizeof(float));
  114.     memcpy(&values[3], &Matrix3_default.cv, 3 * sizeof(float));
  115.     memcpy(&values[6], &Matrix3_default.cw, 3 * sizeof(float));
  116.     code = dict_floats_param(pdref, kstr, 9, values, values);
  117.     if (code < 0)
  118.     return code;
  119.     memcpy(&pmat3->cu, &values[0], 3 * sizeof(float));
  120.     memcpy(&pmat3->cv, &values[3], 3 * sizeof(float));
  121.     memcpy(&pmat3->cw, &values[6], 3 * sizeof(float));
  122.     return 0;
  123. }
  124.  
  125. /* Get 3 procedures from a dictionary. */
  126. int
  127. dict_proc3_param(const ref *pdref, const char *kstr, ref proc3[3])
  128. {
  129.     return dict_proc_array_param(pdref, kstr, 3, proc3);
  130. }
  131.  
  132. /* Get WhitePoint and BlackPoint values. */
  133. int
  134. cie_points_param(const ref * pdref, gs_cie_wb * pwb)
  135. {
  136.     int code;
  137.  
  138.     if ((code = dict_floats_param(pdref, "WhitePoint", 3, (float *)&pwb->WhitePoint, NULL)) < 0 ||
  139.     (code = dict_floats_param(pdref, "BlackPoint", 3, (float *)&pwb->BlackPoint, (const float *)&BlackPoint_default)) < 0
  140.     )
  141.     return code;
  142.     if (pwb->WhitePoint.u <= 0 ||
  143.     pwb->WhitePoint.v != 1 ||
  144.     pwb->WhitePoint.w <= 0 ||
  145.     pwb->BlackPoint.u < 0 ||
  146.     pwb->BlackPoint.v < 0 ||
  147.     pwb->BlackPoint.w < 0
  148.     )
  149.     return_error(e_rangecheck);
  150.     return 0;
  151. }
  152.  
  153. /* Process a 3- or 4-dimensional lookup table from a dictionary. */
  154. /* The caller has set pclt->n and pclt->m. */
  155. /* ptref is known to be a readable array of size at least n+1. */
  156. private int cie_3d_table_param(P4(const ref * ptable, uint count, uint nbytes,
  157.                   gs_const_string * strings));
  158. int
  159. cie_table_param(const ref * ptref, gx_color_lookup_table * pclt,
  160.         gs_memory_t * mem)
  161. {
  162.     int n = pclt->n, m = pclt->m;
  163.     const ref *pta = ptref->value.const_refs;
  164.     int i;
  165.     uint nbytes;
  166.     int code;
  167.     gs_const_string *table;
  168.  
  169.     for (i = 0; i < n; ++i) {
  170.     check_type_only(pta[i], t_integer);
  171.     if (pta[i].value.intval <= 1 || pta[i].value.intval > max_ushort)
  172.         return_error(e_rangecheck);
  173.     pclt->dims[i] = (int)pta[i].value.intval;
  174.     }
  175.     nbytes = m * pclt->dims[n - 2] * pclt->dims[n - 1];
  176.     if (n == 3) {
  177.     table =
  178.         gs_alloc_struct_array(mem, pclt->dims[0], gs_const_string,
  179.                   &st_const_string_element, "cie_table_param");
  180.     if (table == 0)
  181.         return_error(e_VMerror);
  182.     code = cie_3d_table_param(pta + 3, pclt->dims[0], nbytes, table);
  183.     } else {            /* n == 4 */
  184.     int d0 = pclt->dims[0], d1 = pclt->dims[1];
  185.     uint ntables = d0 * d1;
  186.     const ref *psuba;
  187.  
  188.     check_read_type(pta[4], t_array);
  189.     if (r_size(pta + 4) != d0)
  190.         return_error(e_rangecheck);
  191.     table =
  192.         gs_alloc_struct_array(mem, ntables, gs_const_string,
  193.                   &st_const_string_element, "cie_table_param");
  194.     if (table == 0)
  195.         return_error(e_VMerror);
  196.     psuba = pta[4].value.const_refs;
  197.     /*
  198.      * We know that d0 > 0, so code will always be set in the loop:
  199.      * we initialize code to 0 here solely to pacify stupid compilers.
  200.      */
  201.     for (code = 0, i = 0; i < d0; ++i) {
  202.         code = cie_3d_table_param(psuba + i, d1, nbytes, table + d1 * i);
  203.         if (code < 0)
  204.         break;
  205.     }
  206.     }
  207.     if (code < 0) {
  208.     gs_free_object(mem, table, "cie_table_param");
  209.     return code;
  210.     }
  211.     pclt->table = table;
  212.     return 0;
  213. }
  214. private int
  215. cie_3d_table_param(const ref * ptable, uint count, uint nbytes,
  216.            gs_const_string * strings)
  217. {
  218.     const ref *rstrings;
  219.     uint i;
  220.  
  221.     check_read_type(*ptable, t_array);
  222.     if (r_size(ptable) != count)
  223.     return_error(e_rangecheck);
  224.     rstrings = ptable->value.const_refs;
  225.     for (i = 0; i < count; ++i) {
  226.     const ref *const prt2 = rstrings + i;
  227.  
  228.     check_read_type(*prt2, t_string);
  229.     if (r_size(prt2) != nbytes)
  230.         return_error(e_rangecheck);
  231.     strings[i].data = prt2->value.const_bytes;
  232.     strings[i].size = nbytes;
  233.     }
  234.     return 0;
  235. }
  236.  
  237. /* ------ CIE setcolorspace ------ */
  238.  
  239. /* Common code for the CIEBased* cases of setcolorspace. */
  240. private int
  241. cie_lmnp_param(const ref * pdref, gs_cie_common * pcie, ref_cie_procs * pcprocs)
  242. {
  243.     int code;
  244.  
  245.     if ((code = dict_range3_param(pdref, "RangeLMN", &pcie->RangeLMN)) < 0 ||
  246.     (code = dict_proc3_param(pdref, "DecodeLMN", &pcprocs->DecodeLMN)) < 0 ||
  247.     (code = dict_matrix3_param(pdref, "MatrixLMN", &pcie->MatrixLMN)) < 0 ||
  248.     (code = cie_points_param(pdref, &pcie->points)) < 0
  249.     )
  250.     return code;
  251.     pcie->DecodeLMN = DecodeLMN_default;
  252.     return 0;
  253. }
  254.  
  255. /* Common code for the CIEBasedABC/DEF[G] cases of setcolorspace. */
  256. private int
  257. cie_abc_param(const ref * pdref, gs_cie_abc * pcie, ref_cie_procs * pcprocs)
  258. {
  259.     int code;
  260.  
  261.     if ((code = dict_range3_param(pdref, "RangeABC", &pcie->RangeABC)) < 0 ||
  262.     (code = dict_proc3_param(pdref, "DecodeABC", &pcprocs->Decode.ABC)) < 0 ||
  263.     (code = dict_matrix3_param(pdref, "MatrixABC", &pcie->MatrixABC)) < 0 ||
  264.     (code = cie_lmnp_param(pdref, &pcie->common, pcprocs)) < 0
  265.     )
  266.     return code;
  267.     pcie->DecodeABC = DecodeABC_default;
  268.     return 0;
  269. }
  270.  
  271. /* Finish setting a CIE space (successful or not). */
  272. private int
  273. set_cie_finish(i_ctx_t *i_ctx_p, gs_color_space * pcs,
  274.            const ref_cie_procs * pcprocs, int edepth, int code)
  275. {
  276.     if (code >= 0)
  277.     code = gs_setcolorspace(igs, pcs);
  278.     /* Delete the extra reference to the parameter tables. */
  279.     gs_cspace_release(pcs);
  280.     /* Free the top-level object, which was copied by gs_setcolorspace. */
  281.     gs_free_object(gs_state_memory(igs), pcs, "set_cie_finish");
  282.     if (code < 0) {
  283.     ref_stack_pop_to(&e_stack, edepth);
  284.     return code;
  285.     }
  286.     istate->colorspace.procs.cie = *pcprocs;
  287.     pop(1);
  288.     return (ref_stack_count(&e_stack) == edepth ? 0 : o_push_estack);
  289. }
  290.  
  291. /* Forward references */
  292. private int cache_common(P5(i_ctx_t *, gs_cie_common *, const ref_cie_procs *,
  293.                 void *, gs_ref_memory_t *));
  294. private int cache_abc_common(P5(i_ctx_t *, gs_cie_abc *, const ref_cie_procs *,
  295.                 void *, gs_ref_memory_t *));
  296.  
  297. /* <dict> .setciedefgspace - */
  298. private int cie_defg_finish(P1(i_ctx_t *));
  299. private int
  300. zsetciedefgspace(i_ctx_t *i_ctx_p)
  301. {
  302.     os_ptr op = osp;
  303.     int edepth = ref_stack_count(&e_stack);
  304.     gs_memory_t *mem = gs_state_memory(igs);
  305.     gs_ref_memory_t *imem = (gs_ref_memory_t *)mem;
  306.     gs_color_space *pcs;
  307.     ref_cie_procs procs;
  308.     gs_cie_defg *pcie;
  309.     int code;
  310.     ref *ptref;
  311.  
  312.     check_type(*op, t_dictionary);
  313.     check_dict_read(*op);
  314.     if ((code = dict_find_string(op, "Table", &ptref)) <= 0)
  315.     return (code < 0 ? code : gs_note_error(e_rangecheck));
  316.     check_read_type(*ptref, t_array);
  317.     if (r_size(ptref) != 5)
  318.     return_error(e_rangecheck);
  319.     procs = istate->colorspace.procs.cie;
  320.     code = gs_cspace_build_CIEDEFG(&pcs, NULL, mem);
  321.     if (code < 0)
  322.     return code;
  323.     pcie = pcs->params.defg;
  324.     pcie->Table.n = 4;
  325.     pcie->Table.m = 3;
  326.     if ((code = dict_ranges_param(op, "RangeDEFG", 4, pcie->RangeDEFG.ranges)) < 0 ||
  327.     (code = dict_proc_array_param(op, "DecodeDEFG", 4, &procs.PreDecode.DEFG)) < 0 ||
  328.     (code = dict_ranges_param(op, "RangeHIJK", 4, pcie->RangeHIJK.ranges)) < 0 ||
  329.     (code = cie_table_param(ptref, &pcie->Table, mem)) < 0 ||
  330.     (code = cie_abc_param(op, (gs_cie_abc *) pcie, &procs)) < 0 ||
  331.     (code = cie_cache_joint(i_ctx_p, &istate->colorrendering.procs, (gs_cie_common *)pcie, igs)) < 0 ||    /* do this last */
  332.     (code = cie_cache_push_finish(i_ctx_p, cie_defg_finish, imem, pcie)) < 0 ||
  333.     (code = cie_prepare_cache4(i_ctx_p, &pcie->RangeDEFG,
  334.                    procs.PreDecode.DEFG.value.const_refs,
  335.                    &pcie->caches_defg.DecodeDEFG[0],
  336.                    pcie, imem, "Decode.DEFG")) < 0 ||
  337.     (code = cache_abc_common(i_ctx_p, (gs_cie_abc *)pcie, &procs, pcie, imem)) < 0
  338.     )
  339.     DO_NOTHING;
  340.     return set_cie_finish(i_ctx_p, pcs, &procs, edepth, code);
  341. }
  342. private int
  343. cie_defg_finish(i_ctx_t *i_ctx_p)
  344. {
  345.     os_ptr op = osp;
  346.     gs_cie_defg *pcie = r_ptr(op, gs_cie_defg);
  347.  
  348.     pcie->DecodeDEFG = DecodeDEFG_from_cache;
  349.     pcie->common.DecodeLMN = DecodeLMN_from_cache;
  350.     gs_cie_defg_complete(pcie);
  351.     pop(1);
  352.     return 0;
  353. }
  354.  
  355. /* <dict> .setciedefspace - */
  356. private int cie_def_finish(P1(i_ctx_t *));
  357. private int
  358. zsetciedefspace(i_ctx_t *i_ctx_p)
  359. {
  360.     os_ptr op = osp;
  361.     int edepth = ref_stack_count(&e_stack);
  362.     gs_memory_t *mem = gs_state_memory(igs);
  363.     gs_ref_memory_t *imem = (gs_ref_memory_t *)mem;
  364.     gs_color_space *pcs;
  365.     ref_cie_procs procs;
  366.     gs_cie_def *pcie;
  367.     int code;
  368.     ref *ptref;
  369.  
  370.     check_type(*op, t_dictionary);
  371.     check_dict_read(*op);
  372.     if ((code = dict_find_string(op, "Table", &ptref)) <= 0)
  373.     return (code < 0 ? code : gs_note_error(e_rangecheck));
  374.     check_read_type(*ptref, t_array);
  375.     if (r_size(ptref) != 4)
  376.     return_error(e_rangecheck);
  377.     procs = istate->colorspace.procs.cie;
  378.     code = gs_cspace_build_CIEDEF(&pcs, NULL, mem);
  379.     if (code < 0)
  380.     return code;
  381.     pcie = pcs->params.def;
  382.     pcie->Table.n = 3;
  383.     pcie->Table.m = 3;
  384.     if ((code = dict_range3_param(op, "RangeDEF", &pcie->RangeDEF)) < 0 ||
  385.     (code = dict_proc3_param(op, "DecodeDEF", &procs.PreDecode.DEF)) < 0 ||
  386.     (code = dict_range3_param(op, "RangeHIJ", &pcie->RangeHIJ)) < 0 ||
  387.     (code = cie_table_param(ptref, &pcie->Table, mem)) < 0 ||
  388.     (code = cie_abc_param(op, (gs_cie_abc *) pcie, &procs)) < 0 ||
  389.     (code = cie_cache_joint(i_ctx_p, &istate->colorrendering.procs, (gs_cie_common *)pcie, igs)) < 0 ||    /* do this last */
  390.     (code = cie_cache_push_finish(i_ctx_p, cie_def_finish, imem, pcie)) < 0 ||
  391.     (code = cie_prepare_cache3(i_ctx_p, &pcie->RangeDEF,
  392.                    procs.PreDecode.DEF.value.const_refs,
  393.                    &pcie->caches_def.DecodeDEF[0],
  394.                    pcie, imem, "Decode.DEF")) < 0 ||
  395.     (code = cache_abc_common(i_ctx_p, (gs_cie_abc *)pcie, &procs, pcie, imem)) < 0
  396.     )
  397.     DO_NOTHING;
  398.     return set_cie_finish(i_ctx_p, pcs, &procs, edepth, code);
  399. }
  400. private int
  401. cie_def_finish(i_ctx_t *i_ctx_p)
  402. {
  403.     os_ptr op = osp;
  404.     gs_cie_def *pcie = r_ptr(op, gs_cie_def);
  405.  
  406.     pcie->DecodeDEF = DecodeDEF_from_cache;
  407.     pcie->common.DecodeLMN = DecodeLMN_from_cache;
  408.     gs_cie_def_complete(pcie);
  409.     pop(1);
  410.     return 0;
  411. }
  412.  
  413. /* <dict> .setcieabcspace - */
  414. private int cie_abc_finish(P1(i_ctx_t *));
  415. private int
  416. zsetcieabcspace(i_ctx_t *i_ctx_p)
  417. {
  418.     os_ptr op = osp;
  419.     int edepth = ref_stack_count(&e_stack);
  420.     gs_memory_t *mem = gs_state_memory(igs);
  421.     gs_ref_memory_t *imem = (gs_ref_memory_t *)mem;
  422.     gs_color_space *pcs;
  423.     ref_cie_procs procs;
  424.     gs_cie_abc *pcie;
  425.     int code;
  426.  
  427.     check_type(*op, t_dictionary);
  428.     check_dict_read(*op);
  429.     procs = istate->colorspace.procs.cie;
  430.     code = gs_cspace_build_CIEABC(&pcs, NULL, mem);
  431.     if (code < 0)
  432.     return code;
  433.     pcie = pcs->params.abc;
  434.     code = cie_abc_param(op, pcie, &procs);
  435.     if (code < 0 ||
  436.     (code = cie_cache_joint(i_ctx_p, &istate->colorrendering.procs, (gs_cie_common *)pcie, igs)) < 0 ||    /* do this last */
  437.     (code = cie_cache_push_finish(i_ctx_p, cie_abc_finish, imem, pcie)) < 0 ||
  438.     (code = cache_abc_common(i_ctx_p, pcie, &procs, pcie, imem)) < 0
  439.     )
  440.     DO_NOTHING;
  441.     return set_cie_finish(i_ctx_p, pcs, &procs, edepth, code);
  442. }
  443. private int
  444. cie_abc_finish(i_ctx_t *i_ctx_p)
  445. {
  446.     os_ptr op = osp;
  447.     gs_cie_abc *pcie = r_ptr(op, gs_cie_abc);
  448.  
  449.     pcie->DecodeABC = DecodeABC_from_cache;
  450.     pcie->common.DecodeLMN = DecodeLMN_from_cache;
  451.     gs_cie_abc_complete(pcie);
  452.     pop(1);
  453.     return 0;
  454. }
  455.  
  456. /* <dict> .setcieaspace - */
  457. private int cie_a_finish(P1(i_ctx_t *));
  458. private int
  459. zsetcieaspace(i_ctx_t *i_ctx_p)
  460. {
  461.     os_ptr op = osp;
  462.     int edepth = ref_stack_count(&e_stack);
  463.     gs_memory_t *mem = gs_state_memory(igs);
  464.     gs_ref_memory_t *imem = (gs_ref_memory_t *)mem;
  465.     gs_color_space *pcs;
  466.     ref_cie_procs procs;
  467.     gs_cie_a *pcie;
  468.     int code;
  469.  
  470.     check_type(*op, t_dictionary);
  471.     check_dict_read(*op);
  472.     procs = istate->colorspace.procs.cie;
  473.     if ((code = dict_proc_param(op, "DecodeA", &procs.Decode.A, true)) < 0)
  474.     return code;
  475.     code = gs_cspace_build_CIEA(&pcs, NULL, mem);
  476.     if (code < 0)
  477.     return code;
  478.     pcie = pcs->params.a;
  479.     if ((code = dict_floats_param(op, "RangeA", 2, (float *)&pcie->RangeA, (const float *)&RangeA_default)) < 0 ||
  480.     (code = dict_floats_param(op, "MatrixA", 3, (float *)&pcie->MatrixA, (const float *)&MatrixA_default)) < 0 ||
  481.     (code = cie_lmnp_param(op, &pcie->common, &procs)) < 0 ||
  482.     (code = cie_cache_joint(i_ctx_p, &istate->colorrendering.procs, (gs_cie_common *)pcie, igs)) < 0 ||    /* do this last */
  483.     (code = cie_cache_push_finish(i_ctx_p, cie_a_finish, imem, pcie)) < 0 ||
  484.     (code = cie_prepare_cache(i_ctx_p, &pcie->RangeA, &procs.Decode.A, &pcie->caches.DecodeA.floats, pcie, imem, "Decode.A")) < 0 ||
  485.     (code = cache_common(i_ctx_p, &pcie->common, &procs, pcie, imem)) < 0
  486.     )
  487.     DO_NOTHING;
  488.     pcie->DecodeA = DecodeA_default;
  489.     return set_cie_finish(i_ctx_p, pcs, &procs, edepth, code);
  490. }
  491. private int
  492. cie_a_finish(i_ctx_t *i_ctx_p)
  493. {
  494.     os_ptr op = osp;
  495.     gs_cie_a *pcie = r_ptr(op, gs_cie_a);
  496.  
  497.     pcie->DecodeA = DecodeA_from_cache;
  498.     pcie->common.DecodeLMN = DecodeLMN_from_cache;
  499.     gs_cie_a_complete(pcie);
  500.     pop(1);
  501.     return 0;
  502. }
  503.  
  504. /* Common cache code */
  505.  
  506. private int
  507. cache_abc_common(i_ctx_t *i_ctx_p, gs_cie_abc * pcie,
  508.          const ref_cie_procs * pcprocs,
  509.          void *container, gs_ref_memory_t * imem)
  510. {
  511.     int code =
  512.     cie_prepare_cache3(i_ctx_p, &pcie->RangeABC,
  513.                pcprocs->Decode.ABC.value.const_refs,
  514.                &pcie->caches.DecodeABC[0], pcie, imem,
  515.                "Decode.ABC");
  516.  
  517.     return (code < 0 ? code :
  518.         cache_common(i_ctx_p, &pcie->common, pcprocs, pcie, imem));
  519. }
  520.  
  521. private int
  522. cache_common(i_ctx_t *i_ctx_p, gs_cie_common * pcie,
  523.          const ref_cie_procs * pcprocs,
  524.          void *container, gs_ref_memory_t * imem)
  525. {
  526.     return cie_prepare_cache3(i_ctx_p, &pcie->RangeLMN,
  527.                   pcprocs->DecodeLMN.value.const_refs,
  528.                   &pcie->caches.DecodeLMN[0], container, imem,
  529.                   "Decode.LMN");
  530. }
  531.  
  532. /* ------ Internal routines ------ */
  533.  
  534. /* Prepare to cache the values for one or more procedures. */
  535. private int cie_cache_finish1(P1(i_ctx_t *));
  536. private int cie_cache_finish(P1(i_ctx_t *));
  537. int
  538. cie_prepare_cache(i_ctx_t *i_ctx_p, const gs_range * domain, const ref * proc,
  539.           cie_cache_floats * pcache, void *container,
  540.           gs_ref_memory_t * imem, client_name_t cname)
  541. {
  542.     int space = imemory_space(imem);
  543.     gs_for_loop_params flp;
  544.     es_ptr ep;
  545.  
  546.     gs_cie_cache_init(&pcache->params, &flp, domain, cname);
  547.     pcache->params.is_identity = r_size(proc) == 0;
  548.     /*
  549.      * If a matrix was singular, it is possible that flp.step = 0.
  550.      * In this case, flp.limit = flp.init as well.
  551.      * Execute the procedure once, and replicate the result.
  552.      */
  553.     if (flp.step == 0) {
  554.     check_estack(5);
  555.     ep = esp;
  556.     make_real(ep + 5, flp.init);
  557.     ep[4] = *proc;
  558.     make_op_estack(ep + 3, cie_cache_finish1);
  559.     esp += 5;
  560.     } else {
  561.     check_estack(9);
  562.     ep = esp;
  563.     make_real(ep + 9, flp.init);
  564.     make_real(ep + 8, flp.step);
  565.     make_real(ep + 7, flp.limit);
  566.     ep[6] = *proc;
  567.     r_clear_attrs(ep + 6, a_executable);
  568.     make_op_estack(ep + 5, zcvx);
  569.     make_op_estack(ep + 4, zfor);
  570.     make_op_estack(ep + 3, cie_cache_finish);
  571.     esp += 9;
  572.     }
  573.     /*
  574.      * The caches are embedded in the middle of other
  575.      * structures, so we represent the pointer to the cache
  576.      * as a pointer to the container plus an offset.
  577.      */
  578.     make_int(ep + 2, (char *)pcache - (char *)container);
  579.     make_struct(ep + 1, space, container);
  580.     return o_push_estack;
  581. }
  582. /* Note that pc3 may be 0, indicating that there are only 3 caches to load. */
  583. int
  584. cie_prepare_caches_4(i_ctx_t *i_ctx_p, const gs_range * domains,
  585.              const ref * procs,
  586.              cie_cache_floats * pc0, cie_cache_floats * pc1,
  587.              cie_cache_floats * pc2, cie_cache_floats * pc3,
  588.              void *container,
  589.              gs_ref_memory_t * imem, client_name_t cname)
  590. {
  591.     cie_cache_floats *pcn[4];
  592.     int i, n, code = 0;
  593.  
  594.     pcn[0] = pc0, pcn[1] = pc1, pcn[2] = pc2;
  595.     if (pc3 == 0)
  596.     n = 3;
  597.     else
  598.     pcn[3] = pc3, n = 4;
  599.     for (i = 0; i < n && code >= 0; ++i)
  600.     code = cie_prepare_cache(i_ctx_p, domains + i, procs + i, pcn[i],
  601.                  container, imem, cname);
  602.     return code;
  603. }
  604.  
  605. /* Store the result of caching one procedure. */
  606. private int
  607. cie_cache_finish_store(i_ctx_t *i_ctx_p, bool replicate)
  608. {
  609.     os_ptr op = osp;
  610.     cie_cache_floats *pcache;
  611.     int code;
  612.  
  613.     check_esp(2);
  614.     /* See above for the container + offset representation of */
  615.     /* the pointer to the cache. */
  616.     pcache = (cie_cache_floats *) (r_ptr(esp - 1, char) + esp->value.intval);
  617.  
  618.     pcache->params.is_identity = false;    /* cache_set_linear computes this */
  619.     if_debug3('c', "[c]cache 0x%lx base=%g, factor=%g:\n",
  620.           (ulong) pcache, pcache->params.base, pcache->params.factor);
  621.     if (replicate ||
  622.     (code = float_params(op, gx_cie_cache_size, &pcache->values[0])) < 0
  623.     ) {
  624.     /* We might have underflowed the current stack block. */
  625.     /* Handle the parameters one-by-one. */
  626.     uint i;
  627.  
  628.     for (i = 0; i < gx_cie_cache_size; i++) {
  629.         code = float_param(ref_stack_index(&o_stack,
  630.                    (replicate ? 0 : gx_cie_cache_size - 1 - i)),
  631.                    &pcache->values[i]);
  632.         if (code < 0)
  633.         return code;
  634.     }
  635.     }
  636. #ifdef DEBUG
  637.     if (gs_debug_c('c')) {
  638.     int i;
  639.  
  640.     for (i = 0; i < gx_cie_cache_size; i += 4)
  641.         dlprintf5("[c]  cache[%3d]=%g, %g, %g, %g\n", i,
  642.               pcache->values[i], pcache->values[i + 1],
  643.               pcache->values[i + 2], pcache->values[i + 3]);
  644.     }
  645. #endif
  646.     ref_stack_pop(&o_stack, (replicate ? 1 : gx_cie_cache_size));
  647.     esp -= 2;            /* pop pointer to cache */
  648.     return o_pop_estack;
  649. }
  650. private int
  651. cie_cache_finish(i_ctx_t *i_ctx_p)
  652. {
  653.     return cie_cache_finish_store(i_ctx_p, false);
  654. }
  655. private int
  656. cie_cache_finish1(i_ctx_t *i_ctx_p)
  657. {
  658.     return cie_cache_finish_store(i_ctx_p, true);
  659. }
  660.  
  661. /* Push a finishing procedure on the e-stack. */
  662. /* ptr will be the top element of the o-stack. */
  663. int
  664. cie_cache_push_finish(i_ctx_t *i_ctx_p, op_proc_t finish_proc,
  665.               gs_ref_memory_t * imem, void *data)
  666. {
  667.     check_estack(2);
  668.     push_op_estack(finish_proc);
  669.     ++esp;
  670.     make_struct(esp, imemory_space(imem), data);
  671.     return o_push_estack;
  672. }
  673.  
  674. /* ------ Initialization procedure ------ */
  675.  
  676. const op_def zcie_l2_op_defs[] =
  677. {
  678.     op_def_begin_level2(),
  679.     {"1.setcieaspace", zsetcieaspace},
  680.     {"1.setcieabcspace", zsetcieabcspace},
  681.     {"1.setciedefspace", zsetciedefspace},
  682.     {"1.setciedefgspace", zsetciedefgspace},
  683.         /* Internal operators */
  684.     {"1%cie_defg_finish", cie_defg_finish},
  685.     {"1%cie_def_finish", cie_def_finish},
  686.     {"1%cie_abc_finish", cie_abc_finish},
  687.     {"1%cie_a_finish", cie_a_finish},
  688.     {"0%cie_cache_finish", cie_cache_finish},
  689.     {"1%cie_cache_finish1", cie_cache_finish1},
  690.     op_def_end(0)
  691. };
  692.